#ifndef HTTP_PARSER_H
#define HTTP_PARSER_H

#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>

// 内存分配宏定义
#ifndef HTTP_MALLOC
#define HTTP_MALLOC(size) malloc(size)
#endif

#ifndef HTTP_CALLOC
#define HTTP_CALLOC(nmemb, size) calloc(nmemb, size)
#endif

#ifndef HTTP_REALLOC
#define HTTP_REALLOC(ptr, size) realloc(ptr, size)
#endif

#ifndef HTTP_FREE
#define HTTP_FREE(ptr) free(ptr)
#endif

// 固定长度宏定义
#define MAX_VERSION_LEN 16
#define MAX_PATH_LEN 256
#define MAX_STATUS_TEXT_LEN 64
#define MAX_HEADER_NAME_LEN 64
#define MAX_HEADER_VALUE_LEN 256
#define MAX_QUERY_KEY_LEN 64
#define MAX_QUERY_VALUE_LEN 128
#define MAX_BODY_LEN 4096  // 限制正文大小，可根据需要调整

// HTTP报文类型
typedef enum {
    HTTP_TYPE_UNKNOWN,
    HTTP_TYPE_REQUEST,
    HTTP_TYPE_RESPONSE
} http_type_t;

// HTTP方法枚举
typedef enum {
    HTTP_METHOD_GET,
    HTTP_METHOD_POST,
    HTTP_METHOD_PUT,
    HTTP_METHOD_DELETE,
    HTTP_METHOD_HEAD,
    HTTP_METHOD_OPTIONS,
    HTTP_METHOD_CONNECT,
    HTTP_METHOD_TRACE,
    HTTP_METHOD_PATCH,
    HTTP_METHOD_UNKNOWN
} http_method_t;

// HTTP头部字段枚举
typedef enum {
    HTTP_HEADER_UNKNOWN,
    HTTP_HEADER_HOST,
    HTTP_HEADER_USER_AGENT,
    HTTP_HEADER_ACCEPT,
    HTTP_HEADER_CONTENT_TYPE,
    HTTP_HEADER_CONTENT_LENGTH,
    HTTP_HEADER_CONNECTION,
    HTTP_HEADER_COOKIE,
    HTTP_HEADER_SET_COOKIE,
    HTTP_HEADER_LOCATION,
    HTTP_HEADER_REFERER,
    HTTP_HEADER_AUTHORIZATION,
    HTTP_HEADER_CACHE_CONTROL,
    HTTP_HEADER_PRAGMA,
    HTTP_HEADER_EXPIRES,
    HTTP_HEADER_LAST_MODIFIED,
    HTTP_HEADER_IF_MODIFIED_SINCE,
    HTTP_HEADER_ETAG,
    HTTP_HEADER_IF_NONE_MATCH,
    HTTP_HEADER_TRANSFER_ENCODING,
    HTTP_HEADER_UPGRADE,
    HTTP_HEADER_SERVER,
    HTTP_HEADER_CONTENT_DISPOSITION,
    HTTP_HEADER_DATE,
    HTTP_HEADER_RANGE,
    HTTP_HEADER_ACCEPT_ENCODING,
    HTTP_HEADER_ACCEPT_LANGUAGE,
    HTTP_HEADER_CUSTOM // 表示自定义头部
} http_header_field_t;

// HTTP查询参数结构
typedef struct {
    char key[MAX_QUERY_KEY_LEN];
    char value[MAX_QUERY_VALUE_LEN];
} http_query_param_t;

// HTTP头部结构
typedef struct {
    http_header_field_t field;  // 头部字段枚举
    char value[MAX_HEADER_VALUE_LEN]; // 头部值
} http_header_t;

// HTTP报文结构
typedef struct {
    http_type_t type;
    
    // 公共字段
    char version[MAX_VERSION_LEN];
    http_header_t *headers;
    size_t header_count;
    char body[MAX_BODY_LEN];
    size_t body_length;
    
    // 请求特有字段
    http_method_t method;
    char path[MAX_PATH_LEN];    // 原始路径（包含查询参数）
    char base_path[MAX_PATH_LEN]; // 不包含查询参数的路径
    http_query_param_t *query_params; // 查询参数数组
    size_t query_param_count;   // 查询参数数量
    
    // 响应特有字段
    int status_code;
    char status_text[MAX_STATUS_TEXT_LEN];
} http_message_t;

// 解析HTTP报文
http_message_t *http_parse(const char *data, size_t length);

// 序列化HTTP报文
char *http_serialize(const http_message_t *msg, size_t *out_len);

// 释放HTTP消息结构
void http_message_free(http_message_t *msg);

// 获取HTTP方法字符串
const char *http_method_str(http_method_t method);

// 根据键名获取查询参数值
const char *http_get_query_param(const http_message_t *msg, const char *key);

// 根据字段枚举获取头部值
const char *http_get_header(const http_message_t *msg, http_header_field_t field);

// 获取头部字段的字符串表示
const char *http_header_field_str(http_header_field_t field);

#endif // HTTP_PARSER_H